package cn.antho.org.controller;

import cn.antho.basic.anno.PreAuthorize;
import cn.antho.basic.util.AjaxResult;
import cn.antho.basic.util.PageList;
import cn.antho.org.domain.Dictionarydetail;
import cn.antho.org.query.DictionarydetailQuery;
import cn.antho.org.service.IDictionarydetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/tdd")
@PreAuthorize
public class DictionarydetailController {

    @Autowired
    private IDictionarydetailService IDictionarydetailService;

    // 增 or 改
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Dictionarydetail dictionarydetail) {
        if (null == dictionarydetail.getId()) {
            // 增
            IDictionarydetailService.add(dictionarydetail);
        } else {
            // 改
            IDictionarydetailService.update(dictionarydetail);
        }
        return AjaxResult.me().success();
    }

    // 删
    @DeleteMapping("/{id}")
    @PreAuthorize(name = "根据id删除", sn = "tdd:delete")
    public AjaxResult delete(@PathVariable("id") Long id) {
        IDictionarydetailService.delete(id);
        return AjaxResult.me().success();
    }

    // 查所有
    @GetMapping
    public List<Dictionarydetail> findAll() {
        return IDictionarydetailService.findAll();
    }

    // 查单个
    @GetMapping("/{id}")
    @PreAuthorize(name = "根据id查询数组字典明细",sn = "tdd:findById")
    public Dictionarydetail findById(@PathVariable("id") Long id) {
        return IDictionarydetailService.findById(id);
    }

    // 分页查询
    @PostMapping
    @PreAuthorize(name = "分页查询数据字典明细",sn = "tdd:queryByPage")
    public PageList<Dictionarydetail> queryByPage(@RequestBody DictionarydetailQuery query) {
        return IDictionarydetailService.queryByPage(query);
    }

    @PatchMapping
    @PreAuthorize(name = "批量删除数据字典明细",sn = "tdd:batchDel")
    public AjaxResult batchDel(@RequestBody List<Long> ids) {
        IDictionarydetailService.batchDel(ids);
        return AjaxResult.me().success();
    }

}
